﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SAKS.Helpers
{
    public static class MenuHelper
    {
        public static HtmlString Menu(this HtmlHelper helper)
        {
            var sb = new StringBuilder();

            // Create opening unordered list tag
            sb.AppendLine("<ul id='menu'>");

            // Render each top level node
            SiteMapNodeCollection topLevelNodes = SiteMap.RootNode.ChildNodes;
            if (HttpContext.Current.User.IsInRole("Admin"))
            {
                foreach (SiteMapNode node in topLevelNodes)
                {
                    string _class ="";
                    if (SiteMap.CurrentNode != null && (SiteMap.CurrentNode.IsDescendantOf(node) || SiteMap.CurrentNode == node))
                        _class = "selected";
                    sb.AppendFormat("\t\t    <li class='{0}'>",_class);
                    sb.AppendFormat(
                        "<a href='{0}'>{1}</a>", 
                        node.Url, 
                        helper.Encode(node.Title));
                    sb.AppendLine("</li>");
                }
            }
            else
            {
                foreach (SiteMapNode node in topLevelNodes)
                {
                    if (node.Title != "管理")
                    {
                        string _class = "";
                        if (SiteMap.CurrentNode != null && (SiteMap.CurrentNode.IsDescendantOf(node) || SiteMap.CurrentNode == node))
                            _class = "selected";
                        sb.AppendFormat("\t\t    <li class='{0}'>",_class);
                        sb.AppendFormat("<a  href='{0}'>{1}</a>", node.Url, helper.Encode(node.Title));
                        sb.AppendLine("</li>");
                    }
                }
            }

            // Close unordered list tag
            sb.Append("\t\t</ul>");

            return new HtmlString(sb.ToString());
        }
    }

}